<template>
	<view class="login_main">
		<view class="headmain">
			<image src="../static/logo.jpg" mode="" class="imgtou"></image>
		</view>
		<view class="wapiao_font">橘票儿申请获得以下权限</view>
		<view class="wapiao_gray">获得你的公开信息（昵称、头像、地区及性别）</view>
		<!-- <button type="default" class="btn_login" @click="getUserInfo">微信一键登录</button> -->
		<!-- 		<button type="default" class="btn_login" open-type="getUserInfo" @getuserinfo="getUserInfo" withCredentials="true">微信一键登录</button>
		<view class="zanbufont" @click="toBack">暂不登录</view>
		<view class="loginbotm">登录并同意<text class="text-red-new">《挖票网用户协议》</text></view> -->
		<!-- #ifdef MP-WEIXIN -->
		<button type="default" class="btn_login" @tap="getUserProfile" withCredentials="true">微信一键登录</button>
		<!-- #endif -->
		<!-- #ifdef MP-TOUTIAO -->
		<button type="default" class="btn_login" @tap="getUserProfile" withCredentials="true">抖音一键登录</button>
		<!-- #endif -->
		<view class="zanbufont" @tap="toBack">暂不登录</view>
		<!-- <view class="loginbotm">登录并同意<text class="text-red-new">《挖票网用户协议》</text></view> -->
		<Privacy></Privacy>
	</view>
</template>

<script>
import Privacy from '@/pages/components/privacy.vue'
	export default {
		data() {
			return {
				options: {}
			}
		},
		components: {
			Privacy,
		},
		onLoad(options) {
			this.options = options
			this.login()
		},
		methods: {
			// 暂不登录,返回上一页
			toBack() {
				this.$common.toBack(Number(this.options.back) || 1)
			},
			// 获取用户信息
			getUserProfile() {
				let _this = this
				let scene = uni.getStorageSync("otherScene") || ''
				// #ifdef MP-WEIXIN
				
				uni.getUserProfile({ // 获取用户信息
					desc: '获取用户的头像，姓名',
					lang:'zh_CN',
					success: (infoRes) => {
						// console.log('infoRes:', infoRes)
						if(infoRes.userInfo.nickName === '微信用户'){//此时将微信的默认头像设置为我们的默认头像
							infoRes.userInfo.avatarUrl = 'https://img.piaohenduo.com/wapiaoLive/userInfo-defaultAvatar.png'
						}
						_this.$api.register({
							code: _this.$store.state.code,
							encryptedData: encodeURIComponent(infoRes
								.encryptedData),
							iv: encodeURIComponent(infoRes.iv),
							scene:scene+'',
							userinfo: JSON.stringify(infoRes.userInfo)
						}).then(res => {
							_this.$store.commit('setUserInfo', res.data.data
								.userinfo)
							uni.setStorageSync('uInfo', JSON.stringify(_this.$store
								.state.uInfo))
							_this.toBack()
							// uni.navigateTo({
							//     url: '../index/index'
							// })
						}).catch(res => {
							console.log(res)
						})
						
					},
					fail: (err) => {
						console.log(err)
					}
				})
				// #endif
				
				// #ifdef MP-TOUTIAO
				// _this.$store.commit('setUserInfo', infoRes.userInfo)
				// uni.setStorageSync('uInfo', JSON.stringify(infoRes.userInfo))
				// _this.toBack()
				uni.login({
					provider: 'touitiao',
					success: function(result) {
						console.log('app->login():',result)
						_this.$store.commit('setCode', result.code)
						uni.setStorageSync('loginCode', result.code)
						uni.getUserInfo({
							provider:'toutiao',
							success: (res) => {
								console.log(res);
							},
							fail: (fail) => {
								console.log(fail);
							}
						})
					}
				});
				// #endif
			},

			// 登录
			login() {
				let _this = this
				let scene = uni.getStorageSync("scene") || ''
				uni.login({
					provider: 'weixin',
					success: function(loginRes) {
						_this.$store.commit('setCode', loginRes.code)
					}
				});
			},


			//登录
			// getUserInfo() {
			// 	let _this = this
			// 	let scene = uni.getStorageSync("scene") || ''
			// 	uni.login({
			// 		provider: 'weixin',
			// 		success: function(loginRes) {
			// 			_this.$store.commit('setCode',loginRes.code)
			// 			uni.getUserInfo({ // 获取用户信息
			// 				provider: 'weixin',
			// 				success: function(infoRes) {
			// 					_this.$api.register({
			// 						code: loginRes.code,
			// 						encryptedData: encodeURIComponent(infoRes.encryptedData),
			// 						iv: encodeURIComponent(infoRes.iv),
			// 						scene: scene,
			// 						userinfo: infoRes.rawData
			// 					}).then(res => {
			// 						_this.$store.commit('setUserInfo', res.data.data.userinfo)
			// 						uni.setStorageSync('uInfo', JSON.stringify(_this.$store.state.uInfo))
			// 						_this.toBack()
			// 					})
			// 				}
			// 			});
			// 		}
			// 	});
			// }
		}
	}
</script>

<style lang="scss" scoped>
	.login_main {
		width: 100%vw;
		height: 100%vh;

		.headmain {
			height: 230rpx;
			margin-top: 180rpx;
			display: flex;
			align-items: center;
			justify-content: center;
		}

		.imgtou {
			width: 128rpx;
			height: 128rpx;
		}

		.wapiao_font {
			text-align: center;
			font-weight: 700;
			font-size: 32rpx;
			margin-top: 30rpx;
		}

		.wapiao_gray {
			text-align: center;
			font-size: 24rpx;
			margin-top: 30rpx;
			color: #aaa;
		}

		.btn_login {
			background-color: #f1255d;
			color: #fff;
			width: 325rpx;
			display: flex;
			align-items: center;
			justify-content: center;
			font-size: 32rpx;
			height: 80rpx;
			border-radius: 5000rpx;
			width: 85%;
			margin-top: 80rpx;
		}

		.zanbufont {
			color: #aaa;
			text-align: center;
			font-size: 28rpx;
			margin-top: 30rpx;
		}

		.loginbotm {
			position: fixed;
			left: 0;
			bottom: 20rpx;
			color: #aaa;
			font-size: 28rpx;
			text-align: center;
			width: 100%;
		}

		.text-red-new {
			color: #f1255d;
		}
	}
</style>
